<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
global $VM_LANG;
$jconf = new JConfig();
$live_site = $jconf->live_site;
$lg = &JFactory::getLanguage();
$my_lang = $lg->get('backwardlang');
$en = "";
if($my_lang=="english") $en = "en/";

$q = "SELECT * FROM #__vm_country WHERE `country_3_code` = 'SWE' OR `country_3_code` = 'DNK'";
$db = JFactory::getDBO();
$db->setQuery($q);
$country = $db->loadObjectList();

/*echo "<pre>";
var_dump($country);
echo "</pre>";*/

?>
<script type="text/javascript">
$(document).ready(function(){
	/*if($("#extra_field_2").is(':checked')){
		$("#another_address").attr("style","");
	}else
	{
		$("#another_address").attr("style","display:none;");
	};*/
	
	/*$("#extra_field_2").change(function(){
		
		if($("#extra_field_2").is(':checked')){
			$("#another_address").show();

		}else
		{
			$("#another_address").hide();
		}
	});*/
	$("#bt_tilgodkend").click(function(){
		var first_name = $("#first_name").attr("value");
		var last_name = $("#last_name").attr("value");
		var company = $("#company").attr("value");
		var address_1 = $("#address_1").attr("value");
		var zip = $("#zip").attr("value");
		var city = $("#city").attr("value");
		var phone_1 = $("#phone_1").attr("value");
		var email = $("#email").attr("value");
		var country = $("#country").val();
		//alert(country);
		//var extra_field_1 = $("#extra_field_1").attr("value");
		//var extra_field_2 = $("#extra_field_2").attr("checked");
		//var extra_field_3 = $("#extra_field_3").attr("checked");

		var company_2 = $("#phone_2").attr("value") + " " + $("#chelinh").attr("value"); // use phone_2 as company 2	
		var address_2 = $("#address_2").attr("value");
		var extra_field_4 = $("#extra_field_4").attr("value");
		var extra_field_5 = $("#extra_field_5").attr("value");

		if(input_validation())
		{
			my_opt = "option=com_virtuemart&page=shop.userinfo&func=shopperAdd&first_name="+first_name+"&last_name="+last_name+"&company="+company+"&address_1="+address_1+"&zip="+zip+"&city="+city+"&phone_1="+phone_1+"&email="+email+"&country="+country+"&address_2="+address_2+"&extra_field_4="+extra_field_4+"&extra_field_5="+extra_field_5+"&phone_2="+company_2;
    		$.ajax({
    		    type:"POST",
    		    url:"index2.php",
    		    data: my_opt,
    		    success:function(result){
    		       window.location = "index.php?option=com_virtuemart&page=shop.shippingmethod";  			    			  
    		    }
    		});
		}
	});
	$(".bt_5").click(function(){
		window.history.go(-1);
	});

	input_validation = function(){
		var result = true;
		
		msg_first_name = "<p><?php echo $VM_LANG->_('HA_ENTER_FIRST_NAME');?></p>";
		msg_last_name = "<p><?php echo $VM_LANG->_('HA_ENTER_LAST_NAME');?></p>";
		msg_address = "<p><?php echo $VM_LANG->_('HA_PLZ_ENTER_ADDRESS');?></p>";
		msg_zipcode = "<p><?php echo $VM_LANG->_('HA_PLZ_ENTER_ZIPCODE');?></p>";
		msg_zipcode_format = "<p><?php echo $VM_LANG->_('HA_PLZ_ENTER_ZIPCODE_FORMAT');?></p>";
		msg_city = "<p><?php echo $VM_LANG->_('HA_PLZ_ENTER_CITY');?></p>";
		msg_phone = "<p><?php echo $VM_LANG->_('HA_PLZ_ENTER_PHONE_NUMBER');?></p>";
		msg_email = "<p><?php echo $VM_LANG->_('HA_ENTER_EMAIL');?></p>";
		
		if($("#first_name").attr("value").length == 0){
            $("#alert").html(msg_first_name);
            $('#backoverlay').show();
            $('#show_popup').show();
            result = false;
        }
        else
        {
            if($("#last_name").attr("value").length == 0){
                $("#alert").html(msg_last_name);
                $('#backoverlay').show();
                $('#show_popup').show();
                result = false;
            }
            else
            {
                if($("#address_1").attr("value").length == 0){
                    $("#alert").html(msg_address);
                    $('#backoverlay').show();
                    $('#show_popup').show();
                    result = false;
                }
                else
                {
                    post_length = $("#zip").attr("value").length;
                    if( post_length == 0 )
                    {
                        $("#alert").html(msg_zipcode);
                        $('#backoverlay').show();
                        $('#show_popup').show();
                        result = false;
                    }
                    else
                    {
                        if( post_length != 4 )
                        {
                            $("#alert").html(msg_zipcode_format);
                            $('#backoverlay').show();
                            $('#show_popup').show();
                            result = false;
                        }
                        else
                        {
                            if($("#city").attr("value").length == 0){
                                $("#alert").html(msg_city);
                                $('#backoverlay').show();
                                $('#show_popup').show();
                                result = false;
                            }
                            else
                            {
                                if($("#phone_1").attr("value").length == 0)
                                {
                                	$("#alert").html(msg_phone);
                                    $('#backoverlay').show();
                                    
                                    $('#show_popup').show();
                                    result = false;
                                }
                                else
                                {
                                    if($("#email").attr("value").length == 0)
                                    {
                                    	$("#alert").html(msg_email);
                                        $('#backoverlay').show();
                                        $('#show_popup').show();
                                        result = false;
                                    }else {
	                                    if(validateEmail($("#email").attr("value")) == false)
	                                    {
	                                    	$("#alert").html("<?php echo $VM_LANG->_("HA_WRONG_EMAIL_FORMAT"); ?>");
	                                        $('#backoverlay').show();
	                                        $('#show_popup').show();
	                                        result = false;
	                                    }else {
											//alert($("#otherAddress").attr("style"));
											if($("#otherAddress").attr("style") == "")
											{
												//phone_2
												if($("#phone_2").attr("value").length == 0){
													$("#alert").html("<?php echo $VM_LANG->_('HA_ENTER_FIRST_NAME');?>");
			                                        $('#backoverlay').show();
			                                        $('#show_popup').show();
			                                        result = false;
												}else{
													if($("#address_2").attr("value").length == 0){
														$("#alert").html("<?php echo $VM_LANG->_("HA_ENTER_ADDRESS"); ?>");
														$('#backoverlay').show();
														$('#show_popup').show();
														result = false;
													}else{
														post_length2 = $("#extra_field_4").attr("value").length;
														if( post_length2 == 0 )
														{
															$("#alert").html(msg_zipcode);
															$('#backoverlay').show();
															$('#show_popup').show();
															result = false;
														}
														else{
															if( post_length2 != 4 ){
																$("#alert").html(msg_zipcode_format);
																$('#backoverlay').show();
																$('#show_popup').show();
																result = false;
															}
															else{
																if($("#extra_field_5").attr("value").length == 0){
																	$("#alert").html(msg_city);
																	$('#backoverlay').show();
																	$('#show_popup').show();
																	result = false;
																}
															}
														}
														
													}
												}
											}
	                                    }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } 
        return result;
	};

	 validateEmail = function(email) 
	{ 
		 var x=email;
		 var atpos=x.indexOf("@");
		 var dotpos=x.lastIndexOf(".");
		 if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
		 {
		   return false;
		 }
		 return true;
	};

	close_popup=function(){    
        $('#backoverlay').hide();
        $('#show_popup').hide();
    };
    $("#zip").mask("9999",{placeholder:""});
    $("#extra_field_4").mask("9999",{placeholder:""});
    //$("#phone_1").mask("9999999999999999999999999999999999",{placeholder:""});

	$("#first_name").focusout(function(){
		$check_result = "";
		if($("#first_name").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#first_name_check").html($check_result);
	});
	$("#last_name").focusout(function(){
		$check_result = "";
		if($("#last_name").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#last_name_check").html($check_result);
	});
	$("#address_1").focusout(function(){
		$check_result = "";
		if($("#address_1").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#address_1_check").html($check_result);
	});
	$("#company").focusout(function(){
		//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		$("#company_check").html($check_result);
	});
	$("#zip").focusout(function(){
		flag = 0;
		$check_result = "";
		if(($("#zip").attr("value").length == 0) || ($("#zip").attr("value").length < 4)){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
			flag = 1;
		}
		if(flag == 1)
		{
			$("#zip").attr("style","width:45px");
		}
		else
		{
			$("#zip").attr("style","width:47px");
		}
		$("#zip_check").html($check_result);
	});
	$("#city").focusout(function(){
		$check_result = "";
		if($("#city").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#city_check").html($check_result);
	});
	$("#phone_1").focusout(function(){
		$check_result = "";
		if($("#phone_1").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#phone_1_check").html($check_result);
	});
	$("#email").focusout(function(){
		$check_result = "";
		if(($("#email").attr("value").length > 0)&&(validateEmail($("#email").attr("value")) == true)){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#email_check").html($check_result);
	});
	$("#phone_2").focusout(function(){
		$check_result = "";
		//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		$("#phone_2_check").html($check_result);
	});
	$("#address_2").focusout(function(){
		$check_result = "";
		if($("#address_2").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#address_2_check").html($check_result);
	});
	$("#extra_field_4").focusout(function(){
		flag = 0;
		$check_result = "";
		if(($("#extra_field_4").attr("value").length == 0) || ($("#extra_field_4").attr("value").length < 4)){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
			flag = 1;
		}
		if(flag == 1)
		{
			$("#extra_field_4").attr("style","width:45px");
		}
		else
		{
			$("#extra_field_4").attr("style","width:47px");
		}
		$("#extra_4_check").html($check_result);
	});
	$("#extra_field_5").focusout(function(){
		$check_result = "";
		if($("#extra_field_5").attr("value").length > 0){
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_right.png' />";
		}else
		{
			//$check_result=" <img src='<?php echo $live_site?>/templates/rhuk_milkyway/img/ha_wrong.png' />";
		}
		$("#extra_5_check").html($check_result);
	});
});
</script>
<div class="bg_step">
    <div class="p20t">
        <div class="float_left m15l p60r"><a class="bt_step1"></a></div>
        <div class="float_left m15l p60r"><a class="bt_step2 current"></a></div>
        <div class="float_left m30l p60r"><a class="bt_step3"></a></div>
        <div class="float_left m30l"><a class="bt_step4"></a></div>
        <div class="clear_left"></div>
    </div>
</div>

<div class="containerUnder">
	<div class="cartLeft">
		<div class="mainTitle"><?php echo $VM_LANG->_('HA_SHOPPING_CART'); ?></div>
		<ul class="cartCategory">
			<?php 
				for($i=0;$i<count($category);$i++){
					if($category_id != $category[$i]->category_id ){
						echo "<li><a href=\"index.php?option=com_virtuemart&page=shop.list&category_id=".$category[$i]->category_id."&Itemid=".$Itemid."\" class=\"cartCategory\">".$category[$i]->category_name."</a></li>";
					}else{
						echo "<li><a id=\"catSelect\" href=\"index.php?option=com_virtuemart&page=shop.list&category_id=".$category[$i]->category_id."\" class=\"cartCategory\">".$category[$i]->category_name."</a></li>";
					}
				}
			?>
		</ul>
	</div>
	<form id="myformDelivery" action="<?php echo $live_site;?>" method="POST">
	<div class="cartRight">
		<div class="step1">
			<ul>
				<li><p><span>1</span><?php echo $VM_LANG->_('HA_STEP_1'); ?></p></li>
				<li class="select-step"><p><span>2</span><?php echo $VM_LANG->_('HA_STEP_2'); ?></p></li>
				<li><p><span>3</span><?php echo $VM_LANG->_('HA_STEP_3'); ?></p></li>
				<li><p><span>4</span><?php echo $VM_LANG->_('HA_STEP_4'); ?></p></li>
			</ul>
		</div>

			<div class="orderLeft">
			<div class="orderTitle"><!--<span>2</span>--><?php echo strtoupper($VM_LANG->_('HA_USER_INFO_TITLE')); ?> </div>
			<div class="float-left">
				<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_ORDER_PRINT_COMPANY')); ?></div>
				<!--<input class="orderField" type="text"/>-->
				<input id="company" name="company" type="text" value="<?php if($count > 0) echo $my_db->record[0]->company; ?>" class="orderField" /><a id="company_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_FIRST_NAME')); ?> *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="first_name" name="first_name" type="text" value="<?php if($count > 0) echo $my_db->record[0]->first_name; ?>" class="orderField" /><a id="first_name_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_LAST_NAME'));?> *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="last_name" name="last_name" type="text" value="<?php if($count > 0) echo $my_db->record[0]->last_name; ?>" class="orderField" /><a id="last_name_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_ADDRESS_1'));?>  *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="address_1" name="address_1" type="text" value="<?php if($count > 0) echo $my_db->record[0]->address_1; ?>" class="orderField" /><a id="address_1_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_ZIP'));?>*</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="zip" name="zip" type="text" value="<?php if($count > 0) echo $my_db->record[0]->zip; ?>" class="orderField" /><a id="zip_check"></a><input type="hidden" id="ip_holder" value="" />
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_CITY'));?> *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="city" name="city" type="text" value="<?php if($count > 0) echo $my_db->record[0]->city; ?>" class="orderField" /><a id="city_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_COUNTRY'));?></div>
				<select id="country" name="country" class="orderField">
					<?php 
						for($i=0;$i<count($country);$i++){
							if($country[$i]->country_3_code != "DNK"){
								echo '<option value="'.$country[$i]->country_3_code.'">'.$country[$i]->country_name.'</option>';
							}else{
								echo '<option selected="selected" value="'.$country[$i]->country_3_code.'">'.$country[$i]->country_name.'</option>';
							}
						} 
					?>
				</select>
				<div class="clr"></div>
			</div>
			
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_PHONE'));?> *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="phone_1" name="phone_1" type="text" value="<?php if($count > 0) echo $my_db->record[0]->phone_1; ?>" class="orderField" /><a id="phone_1_check"></a>
				<div class="clr"></div>
			</div>
			<div class="orderRow">
				<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_EMAIL'));?> *</div>
				<!--<input class="orderField" type="text"/>-->
				<input id="email" name="email" type="text" value="<?php if($count > 0) echo $my_db->record[0]->user_email; ?>" class="orderField" /><a id="email_check"></a>
				<div class="clr"></div>
			</div>
			<div>
				<a id="bt_tilgodkend" class="btn-til btn float-left mr10" href="#"><span><?php echo $VM_LANG->_('HA_TIL_BETALING');?></span></a>
				<!--a class="btn_send btn float-left" href="#"><span><?php echo $VM_LANG->_('HA_NULLSTIL');?></span></a>-->
			</div>
			</div>
			
			<div class="other-add">
				<a class="float-left btn address-switch btn-other" href="javascript:toggleDiv('otherAddress');"><span><?php echo $VM_LANG->_('HA_SHIPPING_TO_BTN');?></span></a>
				<div id="otherAddress" class="input-set" style="display: none;">
					<div class="space10 clr"></div>
					<!-- <div class="orderRow">
						<div class="orderLabel">Navn*</div>
						<input class="orderField" type="text"/>
						<div class="clr"></div>
					</div>-->
					<?php 
						if($count > 0) {
							$ha_temp = explode(" ",trim($my_db->record[0]->phone_2));
							$chelinh = "";
							for($i = 1; $i<count($ha_temp);$i++){
								$chelinh .= $ha_temp[$i];
							}
							$chelinh = trim($chelinh);
						}
					?>
					<div class="orderRow">
						<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_FIRST_NAME'));?>*</div>
						<!--<input class="orderField" type="text"/>-->
						<input id="phone_2" name="phone_2" type="text" value="<?php if($count > 0) echo $ha_temp[0]; ?>" class="orderField" /><a id="phone_2_check"></a>
						<div class="clr"></div>
					</div>
					
					<div class="orderRow">
						<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_LAST_NAME'));?>*</div>
						<!--<input class="orderField" type="text"/>-->
						<input id="chelinh" name="chelinh" type="text" value="<?php if($count > 0) echo $chelinh; ?>" class="orderField" /><a id="phone_2_check"></a>
						<div class="clr"></div>
					</div>
					<div class="orderRow">
						<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_ADDRESS_1'));?> *</div>
						<!--<input class="orderField" type="text"/>-->
						<input id="address_2" name="address_2" type="text" value="<?php if($count > 0) echo $my_db->record[0]->address_2; ?>" class="orderField" /><a id="address_2_check"></a>
						<div class="clr"></div>
					</div>
					<div class="orderRow">
						<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_ZIP'));?>*</div>
						<!--<input class="orderField" type="text"/>-->
						<input id="extra_field_4" name="extra_field_4" type="text" value="<?php if($count > 0) echo $my_db->record[0]->extra_field_4; ?>" class="orderField" /><a id="extra_4_check"></a><input type="hidden" id="ip_holder" value="" />
						<div class="clr"></div>
					</div>
					<div class="orderRow">
						<div class="orderLabel"><?php echo strtoupper($VM_LANG->_('PHPSHOP_SHOPPER_FORM_CITY'));?> *</div>
						<!--<input class="orderField" type="text"/>-->
						<input id="extra_field_5" name="extra_field_5" type="text" value="<?php if($count > 0) echo $my_db->record[0]->extra_field_5; ?>" class="orderField" /><a id="extra_5_check"></a>
						<div class="clr"></div>
					</div>
				</div>
			</div>
				
		</div>

		

		
		
		<div class="clr"></div>
		<div class="cartButton">
			<div class="cartButtonLeft">
				<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/back_ico.jpg" alt=""/><a onclick="window.history.back();"> <?php echo $VM_LANG->_('HA_BACK_BTN');?></a>
			</div>
			<div class="clr space20"></div>
			<!--<div>
				<a href="#"><img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/footer_banner.jpg" alt=""/></a>
			</div>-->

		</div>
		<div class="cartInfo">
			<div class="cartInfoTxt">Pay by Dankort, Visa, Visa Electron, Mastercard og Maestro</div>
			<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/cart_master1.png" alt=""/>
			<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/cart_master.png" alt=""/>
			<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/cart_visa1.png" alt=""/>
			<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/cart_visa.png" alt=""/>
			<img src="<?php echo JURI::base() ?>templates/rhuk_milkyway/images/cart_DK.png" alt=""/>
			<div class="clr"></div>
		</div>
	</div>
	<div class="clr"></div>
	</form>
	
</div>
<script type="text/javascript">
	function toggleDiv(otherAddress) {
	   $("#otherAddress").toggle();
	}
</script>